import 'dart:convert';

import 'package:dio/dio.dart';
/**
 * 提现页面
 * author wim
 */
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:panda/base/Constant.dart';
import 'package:panda/base/SignType.dart';
import 'package:panda/base/application.dart';
import 'package:panda/http/Api.dart';
import 'package:panda/http/HttpUtil.dart';
import 'package:panda/set/BindZfb.dart';
import 'package:panda/util/AppSaveInfoUtil.dart';
import 'package:panda/util/LoadingUtil.dart';
import 'package:panda/util/SignUtil.dart';

class DrawMoneyPage extends StatefulWidget {
  String total = "0.0";
  DrawMoneyPage({Key key, @required this.total}) : super(key: key);

  @override
  _DrawMoneyPageState createState() => _DrawMoneyPageState();
}

class _DrawMoneyPageState extends State<DrawMoneyPage> {
  String money = "";
  String account = "";
  String name = "";
  String token = "";
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getString(TOKEN).then((value) {
      token = value;
    });
    account = APPLIC.userInfoMap['user_detail']['alipay_account'];
    name = APPLIC.userInfoMap['user_detail']['truename']==null?"":APPLIC.userInfoMap['user_detail']['truename'];
    setState(() {});
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(
        title: Text(
          '余额提现',
          style: TextStyle(
              color: black,
              fontSize: ScreenUtil.getInstance().setSp(BIG_FONT_SIZE)),
        ),
        backgroundColor: white,
        brightness: Brightness.light,
        elevation: 0.2,
        iconTheme: IconThemeData(color: black_99),
      ),
      body: Container(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Padding(
              padding: EdgeInsets.only(
                left: ScreenUtil.getInstance().setHeight(DP_40),
                top: ScreenUtil.getInstance().setHeight(DP_20),
                right: ScreenUtil.getInstance().setHeight(DP_20),
              ),
              child: Text('提现至支付宝',
                  style: TextStyle(
                      color: black,
                      fontSize: ScreenUtil.getInstance().setSp(BIG_FONT_SIZE))),
            ),
            // Container(
            //   child: Row(
            //     mainAxisAlignment: MainAxisAlignment.center,
            //     children: <Widget>[
            //       ClipOval(
            //           child: Container(
            //         width: ScreenUtil.getInstance().setHeight(DP_80),
            //         height: ScreenUtil.getInstance().setHeight(DP_80),
            //         color: white,
            //         child: Image.asset(
            //           "images/app_icon.png",
            //           fit: BoxFit.fill,
            //         ),
            //       )),
            //       Container(padding: EdgeInsets.all(20),child: Text('----------->')),
            //       ClipOval(
            //         child: Container(
            //             color: white,
            //             width: ScreenUtil.getInstance().setHeight(DP_80),
            //             height: ScreenUtil.getInstance().setHeight(DP_80),
            //             child: Image.asset("images/icon_ipay.png",
            //                 fit: BoxFit.fill)),
            //       ),
            //     ],
            //   ),
            // ),
            Container(
                child: Row(
                  children: <Widget>[
                    Text('提现账号       ',
                        style: TextStyle(
                            color: black_99,
                            fontSize: ScreenUtil.getInstance()
                                .setSp(NORMAL_FONT_SIZE))),
                    Expanded(
                      child: Text('$account',
                          style: TextStyle(
                              color: Colors.black54,
                              fontSize: ScreenUtil.getInstance()
                                  .setSp(NORMAL_FONT_SIZE))),
                    ),
                    new Container(
                      width: ScreenUtil.getInstance().setWidth(DP_180),
                      height: ScreenUtil.getInstance().setHeight(DP_40 + 5),
                      margin: EdgeInsets.fromLTRB(0, 0, 0, 8),
                      decoration: new BoxDecoration(
                          color: mainColor,
                          borderRadius: BorderRadius.circular(20.0)),
                      child: new FlatButton(
                        child: new Padding(
                          padding: EdgeInsets.all(0.0),
                          child: new Text(
                            "修改账号",
                            style: new TextStyle(
                                color: Colors.white,
                                fontSize: ScreenUtil.getInstance()
                                    .setSp(SMALL_FONT_SIZE)),
                          ),
                        ),
                        onPressed: () {
                          Navigator.of(context).push<String>(
                              new MaterialPageRoute(builder: (context) {
                            return new BindZfbPage();
                          })).then((value) {
                            if (value != null) {
                              account = value.split(',')[1];
                              name = value.split(',')[0];
                              setState(() {});
                            }
                          });
                        },
                      ),
                    ),
                    // GestureDetector(
                    //   child: Text("修改账号",
                    //       style: TextStyle(
                    //           color: mainColor,
                    //           fontSize: ScreenUtil.getInstance()
                    //               .setSp(NORMAL_FONT_SIZE))),
                    //   onTap: () {

                    //   },
                    //   behavior: HitTestBehavior.opaque,
                    // )
                  ],
                ),
                padding: EdgeInsets.fromLTRB(
                    ScreenUtil.getInstance().setHeight(DP_20),
                    ScreenUtil.getInstance().setHeight(DP_20),
                    ScreenUtil.getInstance().setHeight(DP_20),
                    ScreenUtil.getInstance().setHeight(DP_10)),
                decoration: BoxDecoration(
                    color: white, borderRadius: BorderRadius.circular(0)),
                margin: EdgeInsets.only(
                  left: ScreenUtil.getInstance().setHeight(DP_20),
                  top: ScreenUtil.getInstance().setHeight(DP_20),
                  right: ScreenUtil.getInstance().setHeight(DP_20),
                )),
            Container(height: 1),
            Container(
                child: Row(
                  children: <Widget>[
                    Text('真实姓名       ',
                        style: TextStyle(
                            color: black_99,
                            fontSize: ScreenUtil.getInstance()
                                .setSp(NORMAL_FONT_SIZE))),
                    Expanded(
                      child: Text('$name',
                          style: TextStyle(
                              color: Colors.black54,
                              fontSize: ScreenUtil.getInstance()
                                  .setSp(NORMAL_FONT_SIZE))),
                    ),
                    GestureDetector(
                      child: Text("",
                          style: TextStyle(
                              color: mainColor,
                              fontSize: ScreenUtil.getInstance()
                                  .setSp(NORMAL_FONT_SIZE))),
                      onTap: () {
                        money = widget.total;
                        setState(() {});
                      },
                      behavior: HitTestBehavior.opaque,
                    )
                  ],
                ),
                padding: EdgeInsets.fromLTRB(
                    ScreenUtil.getInstance().setHeight(DP_20),
                    ScreenUtil.getInstance().setHeight(DP_20),
                    ScreenUtil.getInstance().setHeight(DP_20),
                    ScreenUtil.getInstance().setHeight(DP_20)),
                decoration: BoxDecoration(
                    color: white, borderRadius: BorderRadius.circular(0)),
                margin: EdgeInsets.only(
                  left: ScreenUtil.getInstance().setHeight(DP_20),
                  right: ScreenUtil.getInstance().setHeight(DP_20),
                )),
            Container(
              width: double.infinity,
              decoration: BoxDecoration(
                  color: white, borderRadius: BorderRadius.circular(0)),
              padding:
                  EdgeInsets.all(ScreenUtil.getInstance().setHeight(DP_20)),
              margin: EdgeInsets.all(
                ScreenUtil.getInstance().setHeight(DP_20),
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text('输入提现金额',
                      style: TextStyle(
                          color: black,
                          fontSize:
                              ScreenUtil.getInstance().setSp(BIG_FONT_SIZE))),
                  Row(
                    children: <Widget>[
                      Text(MONEY,
                          style: TextStyle(
                              color: black,
                              fontSize: ScreenUtil.getInstance()
                                  .setSp(BIG_FONT_SIZE * 2 - 15))),
                      Expanded(
                        child: TextField(
                          style: TextStyle(
                              fontSize: ScreenUtil.getInstance()
                                  .setSp(BIG_FONT_SIZE * 2 - 15),
                              color: black),
                          keyboardType: TextInputType.number,
                          inputFormatters:[WhitelistingTextInputFormatter.digitsOnly],//只允许输入数字
                          maxLines: 1,
                          controller: TextEditingController.fromValue(
                              TextEditingValue(
                                  text: money, //判断keyword是否为空
                                  // 保持光标在最后

                                  selection: TextSelection.fromPosition(
                                      TextPosition(
                                          affinity: TextAffinity.downstream,
                                          offset: money.length)))),
                          minLines: 1,
                          cursorColor: mainColor,
                          onChanged: (value) {
                            money = value;
                          },
                          keyboardAppearance: Brightness.light,
                          decoration: new InputDecoration(
                            contentPadding: EdgeInsets.all(5),
                            hintText: "0.00",
                            border: InputBorder.none,
                            hintStyle: TextStyle(
                                fontSize: ScreenUtil.getInstance()
                                    .setSp(BIG_FONT_SIZE * 2),
                                color: Colors.black45),
                          ),
                        ),
                      ),
                      // GestureDetector(
                      //   child: Text("全部提现",
                      //       style: TextStyle(
                      //           color: Colors.blue,
                      //           fontSize: ScreenUtil.getInstance()
                      //               .setSp(SMALL_FONT_SIZE))),
                      //   onTap: () {
                      //     money = widget.total;
                      //     setState(() {});
                      //   },
                      //   behavior: HitTestBehavior.opaque,
                      // )
                    ],
                  ),
                  Divider(
                    height: 1.0,
                    indent: 0.0,
                    color: grey,
                  ),
                  Container(
                    height: 10,
                  ),
                  Row(
                    children: <Widget>[
                      Text('本次最多可提现  ',
                          style: TextStyle(
                              color: black,
                              fontSize: ScreenUtil.getInstance()
                                  .setSp(SMALL_FONT_SIZE))),
                      Text(widget.total,
                          style: TextStyle(
                              color: mainColor,
                              fontSize: ScreenUtil.getInstance()
                                  .setSp(NORMAL_FONT_SIZE))),
                    ],
                  )
                ],
              ),
            ),
            // Padding(
            //   padding:
            //       EdgeInsets.all(ScreenUtil.getInstance().setHeight(DP_30)),
            //   child: Text('余额提现至',
            //       style: TextStyle(
            //           color: black,
            //           fontSize: ScreenUtil.getInstance().setSp(BIG_FONT_SIZE))),
            // ),
            // Container(
            //   width: double.infinity,
            //   padding:
            //       EdgeInsets.all(ScreenUtil.getInstance().setHeight(DP_20)),
            //   margin: EdgeInsets.fromLTRB(
            //       ScreenUtil.getInstance().setHeight(DP_20),
            //       0,
            //       ScreenUtil.getInstance().setHeight(DP_20),
            //       ScreenUtil.getInstance().setHeight(DP_20)),
            //   decoration: BoxDecoration(
            //       color: white, borderRadius: BorderRadius.circular(15)),
            //   child: Column(
            //     children: <Widget>[
            //       Row(
            //         children: <Widget>[
            //           Image.asset(
            //             "images/icon_ipay.png",
            //             scale: 1.5,
            //           ),
            //           Container(
            //             width: 10,
            //           ),
            //           Expanded(
            //             child: Text('支付宝',
            //                 style: TextStyle(
            //                     color: black,
            //                     fontSize: ScreenUtil.getInstance()
            //                         .setSp(NORMAL_FONT_SIZE))),
            //           ),
            //           CircularCheckBox(
            //               value: pay_way == "alipay",
            //               activeColor: mainColor,
            //               inactiveColor: black_99,
            //               materialTapTargetSize: MaterialTapTargetSize.padded,
            //               onChanged: (bool x) {
            //                 if (pay_way == "wechat") {
            //                   pay_way = "alipay";
            //                 }
            //                 setState(() {});
            //               }),
            //         ],
            //       ),
            //       //       Container(
            //       //         height: 5,
            //       //       ),
            //       //       Divider(
            //       //         height: 1.0,
            //       //         indent: 0.0,
            //       //         color: grey,
            //       //       ),
            //       //       Container(
            //       //         height: 5,
            //       //       ),
            //       //       Row(
            //       //         children: <Widget>[
            //       //           Image.asset(
            //       //             "images/icon_wechat.png",
            //       //             scale: 1.5,
            //       //           ),
            //       //           Container(
            //       //             width: 10,
            //       //           ),
            //       //           Expanded(
            //       //             child: Text('微信',
            //       //                 style: TextStyle(
            //       //                     color: black,
            //       //                     fontSize: ScreenUtil.getInstance()
            //       //                         .setSp(NORMAL_FONT_SIZE))),
            //       //           ),
            //       //           CircularCheckBox(
            //       //               value: pay_way == "wechat",
            //       //               activeColor: mainColor,
            //       //               inactiveColor: black_99,
            //       //               materialTapTargetSize: MaterialTapTargetSize.padded,
            //       //               onChanged: (bool x) {
            //       //                 if (pay_way == "alipay") {
            //       //                   pay_way = "wechat";
            //       //                 }
            //       //                 setState(() {});
            //       //               }),
            //       //         ],
            //       //       ),
            //     ],
            //   ),
            // ),
            new Container(
              width: double.infinity,
              margin: EdgeInsets.all(ScreenUtil.getInstance().setHeight(DP_40)),
              height: ScreenUtil.getInstance().setWidth(DP_90),
              decoration: new BoxDecoration(
                  color: mainColor, borderRadius: BorderRadius.circular(DP_80)),
              padding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
              child: new FlatButton(
                child: new Padding(
                  padding: EdgeInsets.all(0.0),
                  child: new Text(
                    "立即提现",
                    style: new TextStyle(
                        color: Colors.white,
                        fontSize:
                            ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)),
                  ),
                ),
                onPressed: () {
                  if (account == '') {
                    Fluttertoast.showToast(msg: "请先绑定提现账号");
                    return;
                  }
                  showDialog(
                      context: context,
                      barrierDismissible: false,
                      builder: (_) {
                        return new NetLoadingDialog(
                          loadingText: "提现中...",
                          bgColor: Color.fromARGB(255, 0, 0, 0),
                          requestCallBack: null,
                          outsideDismiss: false,
                        );
                      });
                  put();
                },
              ),
            )
          ],
        ),
      ),
    );
  }

  put() async {
    Map map = new Map();
    map['token'] = token;
    map['account'] = account;
    map['truename'] = name;
    map['account_type'] = "1";
    map['money'] = money;
    map['sign_type'] = SignType.DRAW_MONEY;
    Response<String> response =
        await HttpUtil().post(Api.USER_DRAW, data: getSign(map));
    Map userMap = json.decode(response.data);
    int code = userMap["code"];
    String msg = userMap["msg"];
    Navigator.of(context).pop(1);
    Fluttertoast.showToast(msg: msg,gravity: ToastGravity.CENTER);
    if (code == 1&& mounted) {
      Navigator.of(context).pop();
    }
  }
}
